package com.wsoft.portal.manager;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.service.RedisService;
import com.wsoft.portal.entity.PortalThemeEntity;
import com.wsoft.portal.mapper.PortalThemeMapper;
import com.wsoft.portal.query.theme.ThemeListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/12/27 10:09
 */
@Service
public class PortalThemeManager extends ServiceImpl<PortalThemeMapper, PortalThemeEntity> {

    @Resource
    private RedisService redisService;
    @Resource
    private PortalOrgMemberManager orgMemberManager;

    /**
     * 根据code查询
     * @param code
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Portal.THEME_CODE,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public PortalThemeEntity getByCode(String code) {
        QueryWrapper<PortalThemeEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalThemeEntity::getCode,code);
        return getOne(query);
    }

    /**
     * 根据id修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(PortalThemeEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Portal.THEME_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Portal.THEME_ID + entity.getId());
        }
        return b;
    }

    /**
     * 清除当前系统中的默认主题
     * @return
     */
    public boolean removeDefaultTheme(){
        QueryWrapper<PortalThemeEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalThemeEntity::getIsDefault,1);

        PortalThemeEntity entity = new PortalThemeEntity();
        entity.setIsDefault(0);
        return update(entity,query);
    }


    /**
     * 获取系统中默认主题
     * @return
     */
    public PortalThemeEntity getDefaultTheme(){
        QueryWrapper<PortalThemeEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalThemeEntity::getIsDefault,1);
        return getOne(query);
    }
    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Portal.THEME_ID,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public PortalThemeEntity getById(Long id){
        return super.getById(id);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        PortalThemeEntity entity = getById(id);
        if(entity.getIsDefault() == 1){
            Asserts.fail("[portal.theme.del.error]");
        }
        boolean b = super.removeById(id);
        orgMemberManager.removeByThemeId(id);
        if(b){
            redisService.del(RedisKeyConstant.Portal.THEME_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Portal.THEME_ID + id);
        }
        return b;
    }


    /**
     * 分页查询
     * @param param
     * @return
     */
    public List<PortalThemeEntity> listTheme(ThemeListQuery param) {
        QueryWrapper<PortalThemeEntity> query = new QueryWrapper<>();
        query.lambda()
                .like(StrUtil.isNotBlank(param.getCode()),PortalThemeEntity::getCode,param.getCode())
                .like(StrUtil.isNotBlank(param.getName()),PortalThemeEntity::getName,param.getName())
                .eq(null != param.getIsDefault(),PortalThemeEntity::getIsDefault,param.getIsDefault())
                .eq(null != param.getIsEnable(),PortalThemeEntity::getIsEnable,param.getIsEnable())
                .orderByAsc(PortalThemeEntity::getSort);
        return list(query);
    }
}
